﻿Imports System.ComponentModel
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.Binary

<Serializable()> Public MustInherit Class Layer
    Inherits SerializableObject


    Public Enum FLAYERTYPE
        SHP_POINT
        SHP_POLYLINE
        SHP_POLYGON
        IMG_DEM  '灰度图
        IMG_BMP '彩色位图

    End Enum

    '为了提供序列化能力，而加入的枚举
    'Dim tem As MapWinGIS.tkPointType  =
    <Serializable()> Public Enum FtkFillStipple
        fsCross
        fsCustom
        fsDiagonalDownLeft
        fsDiagonalDownRight
        fsHorizontalBars
        fsNone
        fsPolkaDot
        fsRaster
        fsVerticalBars
    End Enum



    Protected Function getFillType(ByVal ft As FtkFillStipple) As MapWinGIS.tkFillStipple
        Select Case ft

            Case FtkFillStipple.fsCross
                Return MapWinGIS.tkFillStipple.fsCross

            Case FtkFillStipple.fsCustom

                Return MapWinGIS.tkFillStipple.fsCustom

            Case FtkFillStipple.fsDiagonalDownLeft
                Return MapWinGIS.tkFillStipple.fsDiagonalDownLeft

            Case FtkFillStipple.fsDiagonalDownRight
                Return MapWinGIS.tkFillStipple.fsDiagonalDownRight

            Case FtkFillStipple.fsHorizontalBars

                Return MapWinGIS.tkFillStipple.fsHorizontalBars

            Case FtkFillStipple.fsNone
                Return MapWinGIS.tkFillStipple.fsNone

            Case FtkFillStipple.fsPolkaDot

                Return MapWinGIS.tkFillStipple.fsPolkaDot

            Case FtkFillStipple.fsRaster
                Return MapWinGIS.tkFillStipple.fsRaster

            Case FtkFillStipple.fsVerticalBars
                Return MapWinGIS.tkFillStipple.fsVerticalBars

        End Select


    End Function

    <Serializable()> Public Enum FtkLineStipple
        lsCustom
        lsDashDotDash
        lsDashDotDot
        lsDashed
        lsDotted
        lsDoubleSolid
        lsDoubleSolidPlusDash
        lsNone
        lsTrainTracks

    End Enum
    Protected Function getLineType(ByVal lt As FtkLineStipple) As MapWinGIS.tkLineStipple

        Select Case lt
            Case FtkLineStipple.lsCustom

                Return MapWinGIS.tkLineStipple.lsCustom

            Case FtkLineStipple.lsDashDotDash
                Return MapWinGIS.tkLineStipple.lsDashDotDash

            Case FtkLineStipple.lsDashDotDot
                Return MapWinGIS.tkLineStipple.lsDashDotDot

            Case FtkLineStipple.lsDashed
                Return MapWinGIS.tkLineStipple.lsDashed

            Case FtkLineStipple.lsDotted
                Return MapWinGIS.tkLineStipple.lsDotted

            Case FtkLineStipple.lsDoubleSolid
                Return MapWinGIS.tkLineStipple.lsDoubleSolid


            Case FtkLineStipple.lsDoubleSolidPlusDash
                Return MapWinGIS.tkLineStipple.lsDoubleSolidPlusDash

            Case FtkLineStipple.lsNone
                Return MapWinGIS.tkLineStipple.lsNone


            Case FtkLineStipple.lsTrainTracks

                Return MapWinGIS.tkLineStipple.lsTrainTracks


        End Select


    End Function

    <Serializable()> Public Enum FtkPointType
        ptCircle
        ptDiamond
        ptFontChar
        ptImageList
        ptSquare
        ptTriangleDown
        ptTriangleLeft
        ptTriangleRight
        ptTriangleUp
        ptUserDefined
    End Enum
    Protected Function getPointType(ByVal pt As FtkPointType) As MapWinGIS.tkPointType

        Select Case pt
            Case FtkPointType.ptCircle
                Return MapWinGIS.tkPointType.ptCircle


            Case FtkPointType.ptDiamond
                Return MapWinGIS.tkPointType.ptDiamond


            Case FtkPointType.ptFontChar
                Return MapWinGIS.tkPointType.ptFontChar


            Case FtkPointType.ptImageList
                Return MapWinGIS.tkPointType.ptImageList


            Case FtkPointType.ptSquare
                Return MapWinGIS.tkPointType.ptSquare


            Case FtkPointType.ptTriangleDown
                Return MapWinGIS.tkPointType.ptTriangleDown


            Case FtkPointType.ptTriangleLeft
                Return MapWinGIS.tkPointType.ptTriangleLeft


            Case FtkPointType.ptTriangleRight
                Return MapWinGIS.tkPointType.ptTriangleRight


            Case FtkPointType.ptTriangleUp

                Return MapWinGIS.tkPointType.ptTriangleUp


            Case FtkPointType.ptUserDefined

                Return MapWinGIS.tkPointType.ptUserDefined

        End Select

    End Function

    Public Function GetLayerPos() As Integer
        If mapwin IsNot Nothing Then


            Return mapwin.get_LayerPosition(m_iLayer)
        Else

            Return -1

        End If



    End Function

    '构造函数
    Public Sub New(ByVal ln As String, ByVal fn As String)
        MyBase.New()

        lname = ln
        fname = fn

        'ltype = lt

        '间接初始值部分
        ldesc = lname
        m_bShowText = False

        m_TextColor = Drawing.Color.Black
        m_TextFont = New Drawing.Font(New Drawing.FontFamily("Times New Roman"), 8, Drawing.FontStyle.Regular, Drawing.GraphicsUnit.Pixel)


        m_bEditing = False
        m_ShowLayer = True

        layerTransparent = 100




    End Sub

    '这些都是为了讨厌的VB事件逻辑啊
    Protected Sub New( _
      ByVal info As System.Runtime.Serialization.SerializationInfo, _
      ByVal context As System.Runtime.Serialization.StreamingContext)

        MyBase.New(info, context)

    End Sub



    '向外发布的事件模型


    Public Event DescChanged(ByVal ln As String, ByVal ds As String)



    '指示该层是否属于editableshapelayer

    Protected m_Editalbe As Boolean = False

    <Browsable(False)> _
    Public ReadOnly Property IsEditable() As Boolean

        Get

            Return m_Editalbe

        End Get

    End Property

    '指示该层是否可以并且正在编辑



    Protected m_bEditing As Boolean

    <Browsable(False)> _
    Public ReadOnly Property EditingProperty() As Boolean
        Get
            Return m_bEditing
        End Get

    End Property
    Public Sub SetNoEdditing()

        m_bEditing = False
        If mapwin Is Nothing Then

            Return
        End If
        mapwin.MapCursor = MapWinGIS.tkCursor.crsrMapDefault

        setNoEdditingSubRoute()

    End Sub
    Protected Overridable Sub setNoEdditingSubRoute()

    End Sub


    '必要的字段
    Protected m_ShowLayer As Boolean

    <CategoryAttribute("基本信息"), _
    Browsable(False), _
  DisplayName("是否显示层")> _
  Property IsShowLayer() As Boolean
        Get
            Return m_ShowLayer
        End Get
        Set(ByVal value As Boolean)

            m_ShowLayer = value

            OnIsShowLayerChanged()
        End Set
    End Property


    Protected Sub OnIsShowLayerChanged()

        mapwin.set_LayerVisible(m_iLayer, m_ShowLayer)

    End Sub




    Protected lname As String '层名称
    <CategoryAttribute("基本信息"), _
    DisplayName("层ID")> _
    Public ReadOnly Property LnameProperty() As String
        Get
            Return lname
        End Get

    End Property

    Protected fname As String '物理文件位置，以及对应名称
    <CategoryAttribute("基本信息"), _
    Browsable(False), _
    DisplayName("对应文件")> _
    Public Property FnameProperty() As String
        Get
            Return fname
        End Get
        Set(ByVal value As String)

            fname = value
        End Set

    End Property


    'Protected ltype As FLAYERTYPE '层类型
    '<CategoryAttribute("基本信息"), _
    'DisplayName("类型")> _

    'Public ReadOnly Property LTypeProperty() As LAYERTYPE
    '    Get
    '        Return ltype
    '    End Get

    'End Property

    '需要读写的属性
    Protected ldesc As String
    <CategoryAttribute("基本信息"), _
  DisplayName("层描述名称")> _
  Public Property LdescProperty() As String
        Get
            Return ldesc
        End Get
        Set(ByVal value As String)
            ldesc = value

            RaiseEvent DescChanged(Me.lname, ldesc)

        End Set
    End Property


    Dim layerTransparent As Double
    <CategoryAttribute("外观设置"), _
    DisplayName("不透明度")> _
    Public Property LayerTransparentProperty() As Double
        Get
            Return layerTransparent
        End Get
        Set(ByVal value As Double)
            If value > 100 Then
                layerTransparent = 100
                Return
            End If

            If value < 0 Then
                layerTransparent = 0
                Return
            End If
            layerTransparent = value

            Me.OnTransparentChanged(layerTransparent)

        End Set
    End Property

    Private Sub OnTransparentChanged(ByVal d As Double)





        mapwin.set_ImageLayerPercentTransparent(m_iLayer, (1 - d / 100D))

        mapwin.set_ShapeLayerFillTransparency(m_iLayer, d / 100D)

        mapwin.set_ShapeLayerStippleTransparent(m_iLayer, True)


    End Sub



    Protected m_bShowText As Boolean
    <CategoryAttribute("基本信息"), _
  DisplayName("是否显示文本")> _
  Public Property ShowTextProperty() As Boolean
        Get
            Return m_bShowText
        End Get
        Set(ByVal value As Boolean)
            m_bShowText = value

            Me.DrawText()
        End Set
    End Property

    Protected m_TextFont As Drawing.Font
    <CategoryAttribute("基本信息"), _
  DisplayName("文本字体")> _
  Public Property TextFontProperty() As Drawing.Font
        Get
            Return m_TextFont
        End Get
        Set(ByVal value As Drawing.Font)
            m_TextFont = value


            Me.DrawText()
        End Set
    End Property
    Protected m_TextColor As Drawing.Color
    <CategoryAttribute("基本信息"), _
  DisplayName("文本颜色")> _
  Public Property TextColorProperty() As Drawing.Color
        Get
            Return m_TextColor
        End Get
        Set(ByVal value As Drawing.Color)
            m_TextColor = value

            Me.DrawText()
        End Set
    End Property



    '下面的字段必须，在引入后给予初始值
    <NonSerialized()> Public mapwin As AxMapWinGIS.AxMap '内部引用，方便调用


    Public m_iLayer As Integer

    '公共函数部分
    Protected MustOverride Sub DrawText()

    Public MustOverride Sub InitLayer(ByRef m As AxMapWinGIS.AxMap) '用于初始化信息，连接部分事件函数
    Public MustOverride Sub InitProp()
    Protected Sub InitBaseProp()

        Me.OnTransparentChanged(layerTransparent)
        Me.OnIsShowLayerChanged()

    End Sub


    Public MustOverride Sub InitNewLayer(ByRef m As AxMapWinGIS.AxMap, ByVal OutPath As String)



    '公用函数
    Public MustOverride Sub Dispose()









    '最底层的消息分发

    Public Overridable Sub mapMain_MouseDownEvent(ByVal sender As System.Object, ByVal e As AxMapWinGIS._DMapEvents_MouseDownEvent)

    End Sub
    Public Overridable Sub mapMain_MouseMoveEvent(ByVal sender As System.Object, ByVal e As AxMapWinGIS._DMapEvents_MouseMoveEvent)

    End Sub

    Public Overridable Sub mapMain_MouseUpEvent(ByVal sender As System.Object, ByVal e As AxMapWinGIS._DMapEvents_MouseUpEvent)

    End Sub
    Public Overridable Sub mapMain_PreviewKeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs)

    End Sub
    Public Overridable Sub mapMain_DblClick(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Public Overridable Sub mapMain_SelectBoxFinal(ByVal sender As System.Object, ByVal e As AxMapWinGIS._DMapEvents_SelectBoxFinalEvent)

    End Sub
    Public Overridable Sub OnExtentsChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    '下面是关于保存与加载的代码

    Public MustOverride Sub Save()
    Public MustOverride Sub SaveAs(ByVal strPath As String)

    Public MustOverride Sub ReLoad(ByRef m As AxMapWinGIS.AxMap, ByVal fPath As String)



    '关于平等复制预览的函数
    Public Overridable Function GetObject() As Object

        Return mapwin.get_GetObject(m_iLayer)

    End Function



End Class
